package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.bo.KeyWordCreateBO;
import com.project2.bean.po.MarketIssueExample;
import com.project2.bean.po.MarketKeyword;
import com.project2.bean.po.MarketKeywordExample;
import com.project2.mapper.MarketKeywordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

import java.util.Date;
import java.util.List;

@Service
public class KeyWordServiceImpl implements KeyWordService{

    @Autowired
    MarketKeywordMapper keywordMapper;


    @Override
    public CommonData getKeywordInfo(String keyword, String url, PageBo pageBo) {

        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);

        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        if (keyword == null && url == null) {

            List<MarketKeyword> keywordList = keywordMapper.selectByExample(example);

            PageInfo<MarketKeyword> pageInfo = new PageInfo<>(keywordList);

            return CommonData.data(pageInfo);

        }


        if (keyword != null){
            criteria.andKeywordLike("%"+keyword+"%");
        }
        if (url != null){
            criteria.andUrlLike("%"+url+"%");
        }

        List<MarketKeyword> keywordList = keywordMapper.selectByExample(example);

        PageInfo<MarketKeyword> pageInfo = new PageInfo<>(keywordList);

        return CommonData.data(pageInfo);


    }

    @Override
    public MarketKeyword keywordCreate(KeyWordCreateBO k) {

        MarketKeyword keyword = new MarketKeyword();
        keyword.setKeyword(k.getKeyword());
        keyword.setUpdateTime(new Date());
        keyword.setIsDefault(k.getIsDefault());
        keyword.setIsHot(k.getIsHot());
        keyword.setAddTime(new Date());
        keyword.setUrl(k.getUrl());

        int insert = keywordMapper.insertSelective(keyword);

        return keyword;
    }

    @Override
    public int keywordDelete(MarketKeyword key) {
        // 物理删除
        // int num = keywordMapper.deleteByPrimaryKey(keyword.getId());

        //逻辑删除
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(key.getId());

        MarketKeyword keyword = new MarketKeyword();
        keyword.setDeleted(true);
        int num = keywordMapper.updateByExampleSelective(keyword, example);
        return num;
    }

    @Override
    public MarketKeyword keywordUpdate(MarketKeyword keyword) {


        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(keyword.getId());

        keyword.setUpdateTime(new Date());
        int i = keywordMapper.updateByExampleSelective(keyword, example);

        return keyword;
    }
}
